package icegps.com.pingdj.utils;


import android.widget.TextView;

import java.util.ArrayList;

import icegps.com.pingdj.App;
import icegps.com.pingdj.R;

/**
 * Created by 111 on 2018/1/19.
 */

public class UnitUtils {
    public static final int AREA = 1;
    public static final int HEIGHT = 2;
    public static final int LENGTH = 3;
    public static final int SPEED = 4;
    public static final int VOLUME = 5;
    public static final int DEGREES = 6;
    public static final int PERCENT = 7;

    // 1平米 = 1/666.6亩
    public final static float areaRate[] = {
            1f,          // Squaremeter 平方米
            3.3058f,     // ping 坪
            666.66667f,  // Mu 亩
            1000.0f,     // BigMu 大亩
            10000.0f,    // Hectare 公顷
            0.09290304f, // SquareFeet 平方英尺
            4046.856f,   // Acreage 英亩
    };
    public final static float heightRate[] = {
            1.0f,    // Meter 米
            0.3048f, // Feet  英尺
    };

    // 1米 = 1/1000千米
    public final static float lengthRate[] = {
            1.0f,      // Meter 米
            1000.0f,   // Kilometer 公里
            0.3048f,   // Feet 英尺
            1609.344f, // Mile 英里
            1852.0f,   // SeaMile 海里
    };

    // 1 米每秒 = 1.943845 节
    public final static float speedRate[] = {
            1.0f,      // Meter_Sec 米/秒
            0.277778f, // KM_Hr  千里/小时
            0.3048f,   // Feet_Sec 英尺/小时
            0.44704f,  // Mile_Hr 英里/小时
            0.514444f, // Knot 海里/小时
    };

    public final static float volumeRate[] = {
            1.0f,       //立方米
            0.0283168f, //立方英尺
    };

    public final static byte areaPrecision[] = {
            1, // Squaremeter
            1, // ping
            3, // Mu
            3, // BigMu
            4, // Hectare
            0, // SquareFeet
            3, // Acreage
    };

    public final static byte lengthPrecision[] = {
            1, // Meter
            3, // Kilometer
            1, // Feet
            3, // Mile
            3, // SeaMile
    };

    public final static byte volumePrecision[] = {
            1, // 立方米
            0, //立方英尺
    };


    public static ArrayList<String> getUnitValues(int who) {
        ArrayList<String> values = new ArrayList<>();
        switch (who) {
            case AREA:
                values.add(App.getInstance().getString(R.string.squaremeter));
                values.add(App.getInstance().getString(R.string.ping));
                values.add(App.getInstance().getString(R.string.mu));
                values.add(App.getInstance().getString(R.string.bigmu));
                values.add(App.getInstance().getString(R.string.hectare));
                values.add(App.getInstance().getString(R.string.squarefeet));
                values.add(App.getInstance().getString(R.string.acreage));
                break;
            case HEIGHT:
                values.add(App.getInstance().getString(R.string.meter));
                values.add(App.getInstance().getString(R.string.feet));
                break;
            case LENGTH:
                values.add(App.getInstance().getString(R.string.meter));
                values.add(App.getInstance().getString(R.string.kilometer));
                values.add(App.getInstance().getString(R.string.feet));
                values.add(App.getInstance().getString(R.string.mile));
                values.add(App.getInstance().getString(R.string.seamile));
                break;
            case SPEED:
                values.add(App.getInstance().getString(R.string.meter_sec));
                values.add(App.getInstance().getString(R.string.km_hr));
                values.add(App.getInstance().getString(R.string.feet_sec));
                values.add(App.getInstance().getString(R.string.mile_hr));
                values.add(App.getInstance().getString(R.string.knot));

                break;
            case VOLUME:
                values.add(App.getInstance().getString(R.string.stere));
                values.add(App.getInstance().getString(R.string.cubic_feet));
                break;
        }
        return values;
    }

    public static float[] getUnitFloat(int who) {
        switch (who) {
            case AREA:
                return areaRate;
            case HEIGHT:
                return heightRate;
            case LENGTH:
                return lengthRate;
            case SPEED:
                return speedRate;
            case VOLUME:
                return volumeRate;
        }
        return null;
    }

    public static int value2Index(int who, String value) {
        return getUnitValues(who).indexOf(value);
    }

    public static String index2Value(int who, int index) {
        return getUnitValues(who).get(index);
    }


    public static String conversion(int who, int index, double value, int accuracy) {
        return StringUtils.setAccuracy(value / getUnitFloat(who)[index], accuracy);
    }

    public static String conversion(int who, int index, double value) {
        return StringUtils.setAccuracy(value / getUnitFloat(who)[index], 3);
    }


}
